<template>
	<view class="login-container">
		<view class="back-button" @click="goBack">
			<text class="back-icon">←</text>
			<text class="back-text">返回</text>
		</view>
		<text class="greeting">您好</text>
		
		<view class="input-group">
			<view class="input-wrapper">
				<label for="phone" class="input-label">手机号 / 邮箱</label>
				<input 
					id="phone"
					class="input-box" 
					type="text" 
					v-model="phone" 
					placeholder="请输入手机号或邮箱"
					placeholder-class="placeholder-style"
					aria-label="手机号或邮箱输入框"
				/>
			</view>
			
			<view class="password-container">
				<label for="password" class="input-label">密码</label>
				<input 
					id="password"
					class="input-box" 
					:type="showPassword ? 'text' : 'password'" 
					v-model="password" 
					placeholder="请输入密码"
					placeholder-class="placeholder-style"
					aria-label="密码输入框"
				/>
			</view>
			
			<view class="forgot-password">
				<text class="forgot-text" role="button" aria-label="忘记密码">忘记密码</text>
				<text class="verify-code" @click="handleVerifyCode" role="button" aria-label="切换到验证码登录">验证码登录</text>
			</view>
			
			<button class="login-btn" @click="handleLogin" :disabled="!isFormValid" aria-label="登录按钮">登录</button>
		</view>
		
		<view class="register-link">
			<text>还没有账号？</text>
			<text class="register-text" @click="goToRegister" role="button" aria-label="前往注册页面">注册</text>
		</view>
	</view>
</template>

<script>
	import dbService from '../../utils/database.js';
	
	export default {
		data() {
			return {
				phone: '',
				password: '',
				showPassword: false
			}
		},
		computed: {
			isFormValid() {
				return this.phone.length > 0 && this.password.length > 0;
			}
		},
		methods: {
			handleLogin() {
				// 验证表单
				if (!this.phone || !this.password) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none'
					});
					return;
				}

				try {
					// 获取存储的用户数据
					const usersStr = uni.getStorageSync('users');
					const users = usersStr ? JSON.parse(usersStr) : [];
					
					// 检查是否为数组
					if (!Array.isArray(users)) {
						throw new Error('用户数据格式错误');
					}
					
					const user = users.find(u => 
						(u.phone === this.phone || u.email === this.phone) && 
						u.password === this.password
					);
					
					if (user) {
						// 登录成功
						uni.showToast({
							title: '登录成功',
							icon: 'success'
						});
						// 保存当前用户信息
						uni.setStorageSync('currentUser', JSON.stringify(user));
						// 使用switchTab跳转到主页
						uni.switchTab({
							url: '/pages/home/home'
						});
					} else {
						// 登录失败
						uni.showToast({
							title: '账号或密码错误',
							icon: 'error'
						});
					}
				} catch (error) {
					console.error('登录错误:', error);
					uni.showToast({
						title: '登录失败，请重试',
						icon: 'none'
					});
				}
			},
			handleVerifyCode() {
				uni.showToast({
					title: '切换到验证码登录',
					icon: 'none'
				});
			},
			goToRegister() {
				uni.navigateTo({
					url: '/pages/register/register'
				});
			},
			goBack() {
				uni.navigateBack();
			}
		}
	}
</script>

<style>
	.login-container {
		padding: 60rpx 40rpx;
		min-height: 100vh;
		background-color: #FFFFFF;
		position: relative;
	}
	
	.back-button {
		position: absolute;
		top: 60rpx;
		left: 40rpx;
		display: flex;
		align-items: center;
		z-index: 1;
	}
	
	.back-icon {
		font-size: 40rpx;
		color: #333333;
		margin-right: 10rpx;
	}
	
	.back-text {
		font-size: 28rpx;
		color: #333333;
	}
	
	.greeting {
		font-size: 48rpx;
		font-weight: bold;
		margin-bottom: 80rpx;
		display: block;
		margin-top: 80rpx;
	}
	
	.input-group {
		margin-top: 40rpx;
	}
	
	.input-wrapper {
		margin-bottom: 30rpx;
	}
	
	.input-label {
		display: block;
		font-size: 28rpx;
		color: #333333;
		margin-bottom: 10rpx;
		margin-left: 10rpx;
	}
	
	.input-box {
		width: 100%;
		height: 100rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
		padding: 0 30rpx;
		font-size: 32rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
	}
	
	.placeholder-style {
		color: #999999;
	}
	
	.password-container {
		position: relative;
	}
	
	.forgot-password {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 60rpx;
		font-size: 28rpx;
	}
	
	.forgot-text {
		color: #999999;
	}
	
	.verify-code {
		color: #2196F3;
	}
	
	.login-btn {
		width: 100%;
		height: 90rpx;
		line-height: 90rpx;
		background-color: #FF6B00;
		color: #FFFFFF;
		border-radius: 45rpx;
		font-size: 32rpx;
		margin-top: 40rpx;
		border: none;
	}
	
	.login-btn[disabled] {
		opacity: 0.6;
		background-color: #FF6B00;
	}
	
	.register-link {
		position: fixed;
		bottom: 60rpx;
		left: 0;
		right: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 28rpx;
		color: #333333;
	}
	
	.register-text {
		color: #FF6B00;
		margin-left: 10rpx;
	}
</style> 